package queue;

import java.util.Stack;

/**
 * @author : Rookie
 * @description : 用栈实现队列
 * @date : 2023/10/9
 */
public class ImplQueueByStack {

    private final Stack<Integer> stackIn;

    private final Stack<Integer> stackOut;

    private ImplQueueByStack() {
        this.stackIn = new Stack<>();
        this.stackOut = new Stack<>();
    }

    public void offer(int e) {
        stackIn.push(e);
    }

    /**
     * * 出队
     * @return
     */
    public int poll() {
        dumpStackIn();
        return stackOut.pop();
    }

    /**
     * 获取队头元素
     * @return
     */
    public int peek() {
        dumpStackIn();
        return stackOut.peek();
    }

    public void dumpStackIn() {
        if (stackIn.isEmpty()) return;
        while (!stackIn.isEmpty()) {
            stackOut.push(stackIn.pop());
        }
    }

    public static void main(String[] args) {
        int[] input = new int[] {1,2,3,4,5};
        ImplQueueByStack queueByStack = new ImplQueueByStack();
        for (int e:input) {
            queueByStack.offer(e);
        }
        int poll = queueByStack.poll();
        System.out.println("队头元素：" + poll);
        System.out.println(queueByStack.stackOut);
    }
}
